package com.kangfu.api.service.impl;

import com.kangfu.api.service.IUserInfoService;
import com.kangfu.api.service.IUserService;
import com.kangfu.common.exception.BusinessException;
import com.kangfu.db.domain.entity.UserInfo;
import com.kangfu.db.mapper.UserInfoMapper;
import com.kangfu.db.model.request.UserQuestionnaireRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

import static java.util.Arrays.asList;

@Service
public class UserInfoService implements IUserInfoService {

    @Autowired
    public UserInfoMapper userInfoMapper;

    @Override
    public Integer insertUserInfo(int userId, UserQuestionnaireRequest userQuestionnaireRequest) {
        //先检查是否已经填写
        if (userQuestionnaireRequest.getSubType() == null) {
            userQuestionnaireRequest.setSubType(0);
        }

        List<Integer> subTypeList = asList(1,2,3,4);
        List<Integer> needSubList = asList(2,4,6,8,10,12,14,16);
        if (needSubList.contains(userQuestionnaireRequest.getType())) {
            if (!subTypeList.contains(userQuestionnaireRequest.getSubType())) {
                throw new BusinessException("subType参数错误");
            }
        } else {
            userQuestionnaireRequest.setSubType(0);
        }
        UserInfo userInfo = userInfoMapper.selectByUserIdAndSubType(userId, userQuestionnaireRequest.getType(), userQuestionnaireRequest.getSubType());
        if (userInfo != null) {
            //如果已经填写那就直接更新
            userInfo.setContent(userQuestionnaireRequest.getContent());
            return userInfoMapper.updateByUserIdAndType(userInfo);
        } else {
            userInfo = new UserInfo();
            userInfo.setUserId(userId);
            userInfo.setType(userQuestionnaireRequest.getType());

            userInfo.setSubType(userQuestionnaireRequest.getSubType());
            userInfo.setContent(userQuestionnaireRequest.getContent());
            userInfo.setCreateTime(LocalDateTime.now());
            return userInfoMapper.insertUserInfo(userInfo);
        }
    }

    @Override
    public UserInfo selectByUserIdAndSubType(int userId, int type, int subType) {
        List<Integer> subTypeList = asList(1,2,3,4);
        List<Integer> needSubList = asList(2,4,6,8,10,12,14,16);
        if (needSubList.contains(type)) {
            if (!subTypeList.contains(subType)) {
                throw new BusinessException("subType参数错误");
            }
        } else {
            subType = 0;
        }
        return userInfoMapper.selectByUserIdAndSubType(userId, type, subType);
    }

    @Override
    public Integer updateByUserIdAndType(int userId, int id, UserQuestionnaireRequest userQuestionnaireRequest) {
        if (userQuestionnaireRequest.getSubType() == null) {
            userQuestionnaireRequest.setSubType(0);
        }
        List<Integer> subTypeList = asList(1,2,3,4);
        List<Integer> needSubList = asList(2,4,6,8,10,12,14,16);
        if (needSubList.contains(userQuestionnaireRequest.getType())) {
            if (!subTypeList.contains(userQuestionnaireRequest.getSubType())) {
                throw new BusinessException("subType参数错误");
            }
        } else {
            userQuestionnaireRequest.setSubType(0);
        }

        UserInfo userInfo = new UserInfo();
        userInfo.setId(id);
        userInfo.setUserId(userId);
        userInfo.setType(userQuestionnaireRequest.getType());
        userInfo.setSubType(userQuestionnaireRequest.getSubType());
        userInfo.setContent(userQuestionnaireRequest.getContent());
        userInfo.setUpdateTime(LocalDateTime.now());
        return userInfoMapper.updateByUserIdAndType(userInfo);
    }
}
